package com.weixin.engine.interceptor;

import com.weixin.common.dto.UserDTO;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

/**
 * 参数解析器重写
 *
 * @author wangchunhui
 * @date 2023/5/31 15:19
 */
@Component
public class ArgumentResolver implements HandlerMethodArgumentResolver {

    private RequestResponseBodyMethodProcessor handlerMethodArgumentResolver;

    public ArgumentResolver(RequestResponseBodyMethodProcessor handlerMethodArgumentResolver) {
        this.handlerMethodArgumentResolver = handlerMethodArgumentResolver;
    }

    public ArgumentResolver() {
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.getParameterType().equals(UserDTO.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        UserDTO user = new UserDTO();
        user.setUsername("傅");
        user.setPageNum(1);
        user.setPageSize(10);
        return user;
    }
}